#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "caesar.h"

typedef enum {
    OPT_STDIN_MODE,
    OPT_CSTR_MODE,
    OPT_HELP,
    OPT_UNKNOWN,
} Option;

Option check_option(int, char **);
void usage(int, char *);

int main(int argc, char *argv[])
{
    Option option = check_option(argc, argv);
    switch(option)
    {
        case OPT_STDIN_MODE:
            caesar_stdin_mode();
            return 0;
        case OPT_CSTR_MODE:
            char *mesg = argv[1];
            caesar_encrypt_cstr(mesg);
            puts(mesg);
            return 0;
        case OPT_HELP:
            char *prog = argv[0];
            printf("Usage: %s [string]\n", prog);
            return 0;
        case OPT_UNKNOWN:
            char *program = argv[0];
            usage(argc, program);
    }
    return 0;
}

Option check_option(int ac, char *av[])
{
    if(ac == 1)
        return OPT_STDIN_MODE;
    else if(ac == 2)
        if(strncmp(av[1], "--help", strlen(av[1])) == 0)
            return OPT_HELP;
        else
            return OPT_CSTR_MODE;
    else
        return OPT_UNKNOWN;
}

void usage(int ac, char *program)
{
    fprintf(stderr, "Usage: %s [string]\n", program);
    fprintf(stderr, "ERROR: wrong number of arguments: 1 or 0 needed, %d provided\n", ac - 1);
    exit(1);
}
